#!/usr/bin/env python
from __future__ import with_statement
"""
    save_option_parameters.py

"""
import os,sys

# add self to search path for testing
exe_name = os.path.split(sys.argv[0])[1]

    
def canonical (parser, options):
    """
    Make a record of the canonical program parameters so we can rerun things if necessary
    """
    programme_option_parameters = [exe_name]


    store_false_options = set(i.dest for i in parser.option_list if i.action == "store_false")
    store_true_options  = set(i.dest for i in parser.option_list if i.action == "store_true")


    for option, value in options.__dict__.iteritems():
        if option in store_true_options:
            if value:
                programme_option_parameters.append("--" + option)
        elif option in store_false_options:
            if not value:
                programme_option_parameters.append("--" + option)
        elif isinstance(value,str):
            programme_option_parameters.append("--" + option + " '" + str(value) + "'")
        elif isinstance(value,list):
            for v in value:
                programme_option_parameters.append("--" + option + " '" + str(v) + "'")

    programme_option_parameters = " ".join(programme_option_parameters)
    return programme_option_parameters 


def check_mandatory_options (options, mandatory_options, helpstr):
    missing_options = []
    for o in mandatory_options:
        if not getattr(options, o):
            missing_options.append("--" + o)
    if not len(missing_options):
        return

    die_error("Missing mandatory parameter%s: %s.\n\n%s\n\n" % 
                ("s" if len(missing_options) > 1 else "",
                 ", ".join(missing_options),
                 helpstr))

